package io.joern.scanners.c

import io.joern.scanners.*
import io.joern.console.*
import io.joern.macros.QueryMacros.*
import io.shiftleft.semanticcpg.language.*

object GJB_8114_R_1_13_2 extends QueryBundle {

  implicit val resolver: ICallResolver = NoResolve

  @q
  def R_1_13_2(): Query =
    Query.make(
      name = "R_1_13_2",
      author = Crew.Sixpence,
      title = "禁止函数形参与全局变量同名",
      description = """
                      |当函数形参与全局变量同名时，程序员可能会不小心在函数内部修改了全局变量的值，而不是预期的局部变量。
                     """.stripMargin,
      score = 2,
      withStrRep({ cpg =>
        val globalVariableNames = cpg.method.internal.name("<global>").flatMap(_.local).map(_.name).toList
        cpg.parameter.filter(x => globalVariableNames.contains(x.name))
      }),
      tags = List(QueryTags.default),
      codeExamples = CodeExamples(
        List("""
               |int sign = 100;
               |
               |void func(int sign) { //violation
               |    //todo
               |}
               |""".stripMargin),
        List("""
               |int sign = 100;
               |
               |void func(int formal_sign) { //ok
               |    //todo
               |}""".stripMargin)
      )
    )
}
